package com.example.springtransdemo.controller;

import com.example.springtransdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private DataSourceTransactionManager TransactionManager;//事务管理器
    @Autowired
    private TransactionDefinition transactionDefinition;//需要注入的参数
    @Autowired
    private UserService userService;
    @RequestMapping("/registry")
    public Boolean registry(String userName,String password){
        TransactionStatus transactionStatus = TransactionManager.getTransaction(transactionDefinition);//设定了开始状态
        Integer result = userService.insertInfo(userName,password);
        log.info("插入信息: "+result);
        TransactionManager.rollback(transactionStatus);//回滚
        return true;
    }
}
